<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property string $id
 * @property string $name
 * @property float  $price
 * @property string $category_id
 * @property string $createtime
 * @property integer $userid
 * @property integer $cover_image
 * @property string $currency
 */
class Product extends CommonActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product';
	}

	public function getCurrencyUnit(){
		$unit = Utils::getCurrencyUnit($this->currency);
		return $unit?$unit:'$';
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('userid', 'numerical', 'integerOnly'=>true),
			array('price','numerical','min'=>0,'message'=>'价格必须是大于0'),
			array('name, category_id, createtime', 'length', 'max'=>45),
			array('name,category_id,price','required'),
			array('id, name, category_id, createtime, userid', 'safe', 'on'=>'search'),
			array('cover_image', 'file', 'types'=>'jpg', 'maxSize'=>307200, 'allowEmpty'=>TRUE),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'user' => array(self::BELONGS_TO, 'User', 'userid'),
				'cover' => array(self::BELONGS_TO, 'Image', 'cover_image'),
				'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => Yii::t('product','product_name'),
			'category_id' => Yii::t('product','product_category'),
			'price'=>Yii::t('product','product_price'),
			'createtime' => Yii::t('product','product_createtime'),
			'userid' => Yii::t('product','product_userid'),
			'cover_image'=> Yii::t('product','product_cover_image')
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('category_id',$this->category_id,true);
		$criteria->compare('createtime',$this->createtime,true);
		$criteria->compare('userid',$this->userid);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}